<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class User extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //

        $user=\app\common\model\User::select();
        return $this->fetch('index',['user'=>$user]);
    }
    public function useradd()
    {
        return $this->fetch('useradd');
    }

    public function usersave()
    {
        $data=Request::param();
        $rule=[
            'id|编号'=>'require',
            'username|用户名'=>'require',
            'nickname|昵称'=>'require',
            'phone|手机'=>'require',
            'password|密码'=>'require',
            

        ];
        $message=[
            'id'=>'编号不能为空',
            'username'=>'用户名不能为空',
            'nickname'=>'昵称不能为空',
            'phone'=>'手机不能为空',
            'password'=>'密码不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){
            if(\app\common\model\User::insert(['id'=>$data['id'],'username'=>$data['username'],'nickname'=>$data['nickname'],'phone'=>$data['phone'],'password'=>$data['password']]))
            {
                $this->success('添加成功','/admin/user/index');
            }else{
                $this->error('添加失败','/admin/user/index');
            }
        }else{
            $this->error($validate->getError(),'admin/index/index');
        }
    }

    public function delect($id){
        $id=Request::param();
        $id=$id['id'];
        if(\app\common\model\User::where('id',$id)->delete()){
            $this->success('删除成功','admin/goods/index');
        }else{
                $this->error('删除失败','admin/goods/index');
        }
    }

    public function useredit(){
        $id=Request::param();
        $id=$id['id'];
        $data=\app\common\model\User::where('id',$id)->find();
        
       
        // dump($data);
        return $this->fetch('useredit',['data'=>$data]);
    }
    public function usered()
    {
        $data=Request::param();
        
        $rule=[
            'id|编号'=>'require',
            'username|用户名'=>'require',
            'nickname|昵称'=>'require',
            'phone|手机'=>'require',
            'password|密码'=>'require',
            

        ];
        $message=[
            'id'=>'编号不能为空',
            'username'=>'用户名不能为空',
            'nickname'=>'昵称不能为空',
            'phone'=>'手机不能为空',
            'password'=>'密码不能为空',

        ];
        $validate=new \think\Validate($rule,$message);
        if($validate->check($data)){
            $result=[
                'id'=>$data['id'],
                'username'=>$data['username'],
                'nickname'=>$data['nickname'],
                'phone'=>$data['phone'],
                'password'=>$data['password'],
                
                
            ];
            if(\app\common\model\User::where('id',$data['id'])->update($result)){
                $this->success('修改成功','/admin/goods/index');
            }else{
                // dump($res);
                $this->error('修改失败','/admin/goods/index'); 
            }
        }else{
            $this->error($validate->getError(),'admin/goods/index');
        }
    }

}
